﻿using System;
using UnityEngine;


namespace RStudio.UIFramwork
{
    /// <summary>
    /// 模组|UI模块中心   
    /// </summary>
    public class UIModuleCenter
    {
        public UIModuleCenter()
        {
            Instance = this;

            UIModuleQueue = new UIModuleQueue();
            UIModuleAlive = new UIModuleAlive();
            UIModuleInstance = new UIModuleInstance();
            UIModuleStack = new UIModuleStack();

            new AssetLoaderManager();

            UIModuleQueue.OnUIProcessed += OnUIProcessed;
            HandlerUseRecord.OnUIRecord += OnUIRecorded;

            UIModuleQueue.OnUIProcessed += uiEvent =>
            {
                if (uiEvent.UIEventType == UIEventType.Back) return;

                Debug.Log(
                    $"<color=green><b>[Send]</b></color> " +
                    $"{uiEvent.UIEventType} " +
                    $"{(uiEvent.Type != null ? uiEvent.Type : "无Type")} " +
                    $"{(uiEvent.UIBase != null ? uiEvent.UIBase : "无UIBase")}");
            };
            HandlerUseRecord.OnUIRecord += uiEvent =>
            {
                Debug.Log(
                    $"<color=blue><b>[Back]</b></color> " +
                    $"{uiEvent.UIEventType} " +
                    $"{(uiEvent.Type != null ? uiEvent.Type : "无Type")} " +
                    $"{(uiEvent.UIBase != null ? uiEvent.UIBase : "无UIBase")}");
            };


            UICamera = GameObject.FindGameObjectWithTag("UICamera").GetComponent<Camera>();
            Canvas = GameObject.FindGameObjectWithTag("UICanvas").transform.GetComponent<Canvas>();
        }



        [Tooltip("界面队列项")] public UIModuleQueue UIModuleQueue;
        [Tooltip("活跃界面项")] public UIModuleAlive UIModuleAlive;
        [Tooltip("界面实例库")] public UIModuleInstance UIModuleInstance;
        [Tooltip("界面日志库")] public UIModuleEvent UIModuleEvent;
        [Tooltip("栈使用记录")] public UIModuleStack UIModuleStack;
        [Tooltip("界面相机项")] public Camera UICamera;
        [Tooltip("根画布节点")] public Canvas Canvas;


        public static UIModuleCenter Instance { get; private set; }


        /// <summary>
        /// 常规调度|开关
        /// </summary>
        public void SendToQueue(UIEventArgs uiEvent)
        {
            UIModuleQueue.Enqueue(uiEvent);
        }



        private void OnUIRecorded(UIEventArgs uiEvent)
        {
            this.UIModuleStack.Push(uiEvent);
        }
        private void OnUIProcessed(UIEventArgs uiEvent)
        {
            if (uiEvent.UIEventType == UIEventType.Back)
            {
                this.Back();
                return;
            }


            UIModuleInstance.PreLoad(uiEvent.Type);


            if (uiEvent.UIEventType == UIEventType.Open)
            {
                this.Open(uiEvent.Type);
                return;
            }


            if (uiEvent.UIEventType == UIEventType.Close)
            {
                if (uiEvent.UIBase != null)
                {
                    this.Close(uiEvent.UIBase);
                }
                else
                {
                    this.Close(uiEvent.Type);
                }

                return;
            }


            UnityEngine.Debug.LogError($"调度异常：{uiEvent.Type.Name} - {uiEvent.UIEventType}");
        }


        #region 常规|调度逻辑
        private void Open(Type type)
        {
            UIModuleAlive.Append(type);
        }
        private void Close(Type type)
        {
            UIModuleAlive.Remove(type);
        }
        private void Close(UIBase uiBase)
        {
            UIModuleAlive.Remove(uiBase);
        }
        private void Back()
        {
            bool isPeek = this.UIModuleStack.Peek(out UIEventArgs uiEvent);
            if (!isPeek) return;

            if (!uiEvent.UIBase.gameObject.activeSelf)
            {
                UIModuleAlive.Append(uiEvent.Type);
            }
            else
            {
                bool isDone = UIModuleAlive.TryRemove(uiEvent);
                if (isDone)
                {
                    this.UIModuleStack.Pop(out UIEventArgs _);
                }
            }
        }
        #endregion
    }
}
